$(function () {
    //获取id并填充数据
    let id = location.search.split('=')[1]
    setTimeout(() => {
        if (id) {
        $.ajax({
            type: 'get',
            url: Bignews.article_search+'?id='+id,
            dataType: 'json',
            headers: headers,
            success: function (res) {
                console.log(res);
                $('#inputTitle').val(res.data.title)
                $('.article_cover').attr('src', res.data.cover)
                $('#postDate').val(res.data.date)
                // $('#mytextarea').html('aaa')
                $('.category').val(res.data.categoryId)
                tinymce.activeEditor.setContent(res.data.content)
            }
        })
    }
    }, 1500);

    //动态生成文章分类下拉列表
    $.ajax({
        type:'get',
        url: Bignews.category_list,
        // data:{},
        headers: headers,
        dataType: 'json',
        success: function (res) {
            // console.log(res);
            $('.category').html(template('cateTemp',res.data))
            
        }
    })
    // 初始化fedatae
    jeDate("#postDate",{
        isinitVal:true,
        festival: true,
        maxDate:new Date().toLocaleDateString(),  
        format: "YYYY-MM-DD"
    });
    // 初始化富文本框
    tinymce.init({
            selector: '#mytextarea',
            height:'350px',
            language: 'zh_CN',
            directionality: 'ltl',
            browser_spellcheck: true,
            contextmenu: false,
            plugins: [
                "advlist autolink lists link image charmap print preview anchor",
                "searchreplace visualblocks code fullscreen",
                "insertdatetime media table contextmenu paste imagetools wordcount",
                "code"
            ],
            toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code",
    });
    
    //图片本地预览
    $('#inputCover').on('change', function () {
        let cover = $(this)[0].files[0]
        if (cover.type.indexOf('image/') == -1) {
            alert('请上传图片文件')
        }
        let myurl =URL.createObjectURL(cover)
        $('.article_cover').attr('src',myurl)
    })

    //文章发表或存草稿函数
    function artPost(state) {
        let formdata = new FormData()
        // //1,获取标题内容
        let title = $('#inputTitle').val()
        if (title.trim().length === 0) {
            alert('请输入标题')
            return
        }
        // //2,获取封面文件
        let cover = $('#inputCover')[0].files[0]
        // console.log(cover);
        if (!id) {
            if (!cover) {
                alert('请上传封面')
                return
            }
            if (cover.type.indexOf('image/') == -1) {
                alert('请上传图片文件')
            } 
        } else {
            formdata.append('id', id)
            if (cover && cover.type.indexOf('image/') != -1) {
                formdata.append('cover', cover)
            } else if(cover && cover.type.indexOf('image/') == -1) {
                alert('请上传图片文件')
                return
            }} 
        //3,分类数据的获取
        let categoryId = $('.category').val()
        // console.log(categoryId);

        //4,获取日期值
        let date = $('#postDate').val()

        //5,获取文章内容
        let content = tinymce.activeEditor.getContent()
        // console.log(content);
        if (content.length === 0) {
            alert('请输入文章内容')
            return
        }


        
        formdata.append('title', title)
        formdata.append('categoryId', categoryId)
        formdata.append('date', date)
        formdata.append('content', content)
        formdata.append('state', state)

        //发起ajax请求
        $.ajax({
            type: 'post',
            //使用三元表达式
            url: id ? Bignews.article_edit :  Bignews.article_publish,
            data: formdata,
            headers: headers,
            dataType: 'json',
            contentType: false,
            processData: false,
            success: function (res) {
                alert(res.msg)
                location.href = './article_list.html'
            }
        })
    }
    //发表文章
    $('#publish').on('click', function (e) {
        //阻止默认刷新
        e.preventDefault()
        artPost('已发布')
    })

    //存为草稿
    $('#draft').on('click', function (e) {
        //阻止默认刷新
        e.preventDefault()
        artPost('草稿')
    })
    
})